<template>
    <page-wrapper>
        <table-search @search="search(1)" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-input v-model.trim="form.fileName" placeholder="文件名称" />
                </form-item>
                <form-item label="">
                    <a-tree-select
                        v-model="form.deptName"
                        allowClear
                        style="width:240px"
                        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                        :tree-data="deptList"
                        :replaceFields="{children:'children',
                        title:'deptName',
                        key:'deptId',
                        value: 'deptName' }"
                        placeholder="请选择所属部门"
                    >
                    </a-tree-select>
                </form-item>
                <form-item label="">
                    <a-input v-model.trim="form.owener" placeholder="上传人" />
                </form-item>
                <form-item label="">
                    <a-input v-model.trim="form.keyWord" placeholder="关键词搜索" />
                </form-item>
            </template>
            <template slot="extra">
                <a-button
                    class="filter-item"
                    type="primary"
                    :disabled="hasPerm(['sys:file:add'])"
                    icon="plus"
                    @click="addInfo()">新增文件</a-button>
            </template>
        </table-search>
        <div class="">
            <table-list
                :loading="loading"
                :columns="columns"
                rowKey="id"
                :pagination="pagination"
                :data="list"
            >

                <template slot="type" slot-scope="{ text }">
                    <a-button
                        size="small"
                        class="btn_space"
                        @click="previewHandler(text)"
                        >预览</a-button
                    >
                </template>
                <template slot="deptId" slot-scope="{ text }">
                    <field-tag
                        :value="text"
                        type="deptId"
                        showType="deptName"
                        :data="expandDeptData"
                    ></field-tag>
                </template>
                <template slot="action" slot-scope="{ text }">
                    <a-button
                        size="small"
                        type="primary"
                        :disabled="hasPerm(['sys:train:update']) || accountInfo.userName!=text.owener"
                        @click="addInfo(text, index)"
                        >编辑</a-button
                    >
                    <a-button
                        size="small"
                        class="btn_space"
                        @click="download(text)"
                        >下载</a-button
                    >
                    <a-button
                        type="danger"
                        size="small"
                        class="btn_space"
                        :disabled="hasPerm(['sys:train:update']) || accountInfo.userName!=text.owener"
                        @click="remove(text)"
                    >
                        删除
                    </a-button>
                </template>
            </table-list>
        </div>

        <template v-if="isShowInfo">
            <train-info :info="itemInfo" :deptList="deptList" @cancel="isShowInfo = false" @submit="submit" />
        </template>
        <template v-if="isShowPreview">
            <file-preview :file="previewFile"  @cancel="isShowPreview = false" />
        </template>
        
    </page-wrapper>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './columns';
import TrainInfo from './components/info';
import {remoteList,deleteInfo} from '@/api/system/file'
import deptApi from '@/api/system/dept';
import FilePreview from './components/file-preview.vue';
import { mapState } from 'vuex';

export default {
    name: 'TrainList',
    mixins: [table],
    components: {
        TrainInfo,
        FilePreview
    },
    data() {
        let originForm = {
            keyWord: '',
            fileName:'',
            owener:"",
            deptName:undefined,
        };
        return {
            originForm,
            form: {
                ...originForm
            },
            columns,
            itemInfo: null,
            isShowInfo: false,
            deptList:[],
            expandDeptData:[],
            isShowPreview:false,
            previewFile:{}
        };
    },
    computed: {
        ...mapState({
            accountInfo: state => state.user.accountInfo
        })
    },
    created(){
        this.getResource();
        this.search(1)
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if(page){
                    this.form.page = page
                }
                this.list = []
                let res = await remoteList(this.form);
                let { list, total } = res.data;
                this.list = list;
                this.total = total;
                this.selectionBox = []
            } finally {
                this.loading = false;
            }
        },
        //删除
        remove(item = '') {
            this.$confirm({
                title: '提示',
                content: '文件删除后不可恢复，请谨慎操作！',
                okText: '确定',
                cancelText: '取消',
                onOk:async ()=>{
                    try{
                        let ids=[];
                        if (item) {
                            ids = [item.id];
                        } else {
                            let selectionBox = this.selectionBox;
                            selectionBox.map(async item => {
                                ids.push(item.id);
                            });
                        }
                        await deleteInfo({ids});
                        this.$message.success('删除成功!');
                        this.search(1);
                    }catch(err){

                    }
                },
                onCancel() {}
            });
        },
        previewHandler(row){
            this.isShowPreview = true
            this.previewFile = row;
        },
        //显示添加
        addInfo(item = {}) {
            this.isShowInfo = true;
            this.itemInfo = item;
        },
        submit() {
            this.isShowInfo = false;
            this.itemInfo = {}
            this.search(1);
        },
        download(info){
            let a = document.createElement('a')
            a.href =info.fileUrl;
            a.click();
        },
        //获取资源列表信息
        async getResource(){
            let deptResult = await deptApi.remoteList();
            this.deptList = deptResult.data;
            this.expandDeptData = [];
            this.expandData(deptResult.data);
        },
        //多层次数据展开
        expandData(data){
            if(Array.isArray(data)){
                data.map(item=>{
                    this.expandDeptData.push(item);
                    if(item.children && item.children.length){
                        this.expandData(item.children)
                    }
                })
            }
        }
    }
};
</script>
